package cn.kgc.springboot06.controller;

import cn.kgc.springboot06.entity.FileVO;
import cn.kgc.springboot06.service.FileService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author: mosin
 * @version: v1.0  2022/5/23
 */
@Controller
@RequestMapping("file")
//@CrossOrigin  // 允许跨域的请求
public class FileController {
    @Autowired
    private FileService fileService ;
    @RequestMapping("upload")
    public  String  fileUpload(MultipartFile file, Model model){
        //判定是否有文件上传
        if(file.isEmpty()){
            model.addAttribute("msg", "请选择上传的文件");
            return "upload";
        }

        // 获取文件名  生成文件上传名字 uuid
        String originalFilename = file.getOriginalFilename();
        //获取文件扩展名
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        //获取uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //使用uuid拼接文件上传的名字
        String uuidName = uuid+"."+filenameExtension;

        //文件上传
        File f = new File("D:\\upload");
        if(!f.exists()){
            f.mkdir();
        }
        try {
            //1. 将文件的名字和uuid存入数据库 以便文件的下载展示信息
            FileVO fileVO = new FileVO(null, originalFilename, uuidName);
            fileService.addFile(fileVO);
            // 2. 将文件保存在本地磁盘
            file.transferTo(new File(f, uuidName));
            return "redirect:/file/todownload";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "文件上传失败!");
            return "upload";
        }
    }


    @RequestMapping("download")
    public  Object  fileDownload(String fileName, HttpServletResponse response){
        File file = new File("D:\\upload", fileName);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //设置响应格式 以附件形式打开
            response.setHeader("Content-disposition", "attachment; filename="+fileName);
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.copy(fileInputStream, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    @RequestMapping("todownload")
    public  String toDownload(Model model){
        // 查新所有的下载列表  跳转到下载页
        List<FileVO> fileVOs = fileService.selectAll();
        model.addAttribute("fileVOs", fileVOs);
        return "download";
    }


}
